package net.seehope.foodie.controller.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


//注意这里跟Intercepter的区别
public class TimeFilter implements Filter{
	
	
	private static final Logger log = LoggerFactory.getLogger(TimeFilter.class);


	//要将Filter配置到容器当中，才能起作用
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//记录启动时间
		long start = System.currentTimeMillis();
		
		log.info("timeFilterStart at:" + start);
		
		
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		
		//放行
		chain.doFilter(request, response);
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		long end = System.currentTimeMillis();
		
		log.info(httpServletRequest.getRequestURI() + "timeFilterEnd at:" + end);
		
	}

}
